package com.ruoyi.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.bo.PProblemTopicBo;
import com.ruoyi.system.domain.vo.PProblemTopicVo;
import com.ruoyi.system.listener.TopicImportListener;
import com.ruoyi.system.service.IPProblemTopicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 题库Controller
 *
 * @author ruoyi
 * @date 2022-07-28
 */
@Validated
@Api(value = "题库控制器", tags = {"题库管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/problemTopic")
public class PProblemTopicController extends BaseController {

    private final IPProblemTopicService iPProblemTopicService;

    /**
     * 查询题库列表
     */
    @ApiOperation("查询题库列表")
    @SaCheckPermission("system:problemTopic:list")
    @GetMapping("/list")
    public TableDataInfo<PProblemTopicVo> list(PProblemTopicBo bo, PageQuery pageQuery) {
        return iPProblemTopicService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出题库列表
     */
    @ApiOperation("导出题库列表")
    @SaCheckPermission("system:problemTopic:export")
    @Log(title = "题库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PProblemTopicBo bo, HttpServletResponse response) {
        List<PProblemTopicVo> list = iPProblemTopicService.queryList(bo);
        ExcelUtil.exportExcel(list, "题库", PProblemTopicVo.class, response);
    }

    @ApiOperation("导入题库")
    @PostMapping("/importData")
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport, Long topicId) throws Exception {
        ExcelResult<PProblemTopicBo> result = ExcelUtil.importExcel(file.getInputStream(), PProblemTopicBo.class, new TopicImportListener(updateSupport, topicId));
        return R.ok(result.getAnalysis());
    }

//    @ApiOperation("下载导入模板")
//    @PostMapping("/importTemplate")
//    public void importTemplate(HttpServletResponse response) {
//        ExcelUtil.exportExcel(new ArrayList<>(), "用户数据", SysUserImportVo.class, response);
//    }
//
    /**
     * 获取题库详细信息
     */
    @ApiOperation("获取题库详细信息")
    @SaCheckPermission("system:problemTopic:query")
    @GetMapping("/{id}")
    public R<PProblemTopicVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(iPProblemTopicService.queryById(id));
    }

    /**
     * 新增题库
     */
    @ApiOperation("新增题库")
    @SaCheckPermission("system:problemTopic:add")
    @Log(title = "题库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PProblemTopicBo bo) {
        return toAjax(iPProblemTopicService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改题库
     */
    @ApiOperation("修改题库")
    @SaCheckPermission("system:problemTopic:edit")
    @Log(title = "题库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PProblemTopicBo bo) {
        return toAjax(iPProblemTopicService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除题库
     */
    @ApiOperation("删除题库")
    @SaCheckPermission("system:problemTopic:remove")
    @Log(title = "题库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iPProblemTopicService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
